package com.cake.liulanxiangzhu.utils;

import com.alibaba.fastjson.JSON;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.security.core.GrantedAuthority;

import java.util.*;

public class JwtUtil {

    public static String generateToken(Integer id,String username, String uuid, Collection<GrantedAuthority> authorities ){

        Map<String, Object> claims = new HashMap<>();
        claims.put("id", id);
        claims.put("username", username);
        claims.put("uuid",uuid);
        claims.put("authorities", JSON.toJSONString(authorities));
        String secretKey = "liulanxiangzhushidacake";
        Date expirationDate = new Date(System.currentTimeMillis() + 7 * 24 * 60 * 60 * 1000);//设置token的过期时间
        String jwtToken = Jwts.builder().setHeaderParam("alg", "HS256").setHeaderParam("typ", "JWT")
                .setClaims(claims)
                .setExpiration(expirationDate).signWith(SignatureAlgorithm.HS256, secretKey)
                .compact();
        return jwtToken;
    }

}
